package com.crm.system.controller.contractManager;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.contractManager.CustomerContract;
import com.crm.model.entity.customerManager.Customer;
import com.crm.model.entity.finishOrderProduct.FinishOrderProduct;
import com.crm.model.entity.orderManager.CustomerProductOrderDetail;
import com.crm.model.entity.productManager.CustomerMemberProduct;
import com.crm.model.qo.contractManager.CustomerContractAddQO;
import com.crm.model.qo.contractManager.UpdateContractAddQO;
import com.crm.model.vo.contractReviewManager.UpdateContractVO;
import com.crm.model.vo.customerContractManager.ContractLastThreeMonthVO;
import com.crm.model.vo.customerContractManager.ContractRenewalAddVO;
import com.crm.model.vo.customerContractManager.CustomerContractDetailVO;
import com.crm.system.service.contractManager.CustomerContractService;
import com.crm.system.service.contractManager.UpgradeContractService;
import com.crm.system.service.customerManager.CustomerService;
import com.crm.system.service.finishOrderProductManager.FinishOrderProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 补款升级合同
 * @author luojie
 * @date 2021/9/22 15:09
 */
@RestController
@RequestMapping("/upgradeContract")
@Slf4j
public class UpgradeContractController {

    @Autowired
    private UpgradeContractService upgradeContractService;
    @Autowired
    private CustomerContractService customerContractService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private FinishOrderProductService finishOrderProductService;

    /**
     * 点击补款升级跳转补款升级页面-查询当前产品
     * @param contractId ---合同ID
     * @author luojie
     * @return
     */
    @GetMapping("/selectOldContractProductByContractId")
    public DataResponse selectOldContractProductByContractId(@RequestParam(value="contractId",required = true)String contractId,
                                                             @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                             @RequestParam(value = "pageSize", required = true) Integer pageSize){
        DhPageInfo<FinishOrderProduct> result = upgradeContractService.selectOldContractProductByContractId(contractId,pageIndex,pageSize);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),result);
    }

    /***
     * @Description: 查询除了当前合同产品外的所有会员产品
     * @Param: contractId 合同id
     * @Param: productName 产品名称
     * @Author: luojie
     * @Date: 2021/9/22 16:52
     */
    @GetMapping("/selectUpgradeMemberProductList")
    public DataResponse selectUpgradeMemberProductList(@RequestParam(value="contractId",required = true)String contractId,
                                                       @RequestParam(value="productName",required = false)String productName,
                                                       @RequestParam(value="whetherRenew",required = false)String  whetherRenew){

        List<CustomerMemberProduct> result = upgradeContractService.selectUpgradeMemberProductList(contractId,productName,whetherRenew);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),result);
    }

    /***
     * @Description:新增补款升级合同
     * @Param:
     * @Author: luojie
     * @Date: 2021/9/23 9:20
     */
    @PostMapping("/addUpgradeContract")
    public DataResponse addUpgradeContract(@RequestBody UpdateContractAddQO updateContractAddQO){
        ContractRenewalAddVO oldContract = customerContractService.selectContractById(updateContractAddQO.getContractId());
        //如果合同的支付方式为3:余额支付;4:转入额支付;5:余额＋转入额支付，则判断客户响应金额
        String paymentMethod = updateContractAddQO.getPaymentMethod();
        if(paymentMethod != null && !paymentMethod.equals("2")){
            //查询客户信息
            Customer customer = customerService.selectCustomerById(oldContract.getCustomerId());
            switch(paymentMethod){
                case "3":{
                    if(customer.getCustomerWallet()==null||(customer.getCustomerWallet()!=null&&customer.getCustomerWallet().compareTo(updateContractAddQO.getReplenishmentAmount())==-1)){
                        return new DhResponse(ResponseCodeEnum.CONTRACT_INSUFFICIENT_BALANCE.getResponseCode());
                    }
                    break;
                }
                case "4":
                    if(customer.getCustomerShiftWallet()==null||(customer.getCustomerShiftWallet()!=null&&customer.getCustomerShiftWallet().compareTo(updateContractAddQO.getReplenishmentAmount())==-1)){
                        return new DhResponse(ResponseCodeEnum.CONTRACT_INSUFFICIENT_BALANCE.getResponseCode());
                    }
                    break;
                case "5":
                    if(customer.getCustomerShiftWallet()!=null&&(customer.getCustomerWallet().add(customer.getCustomerShiftWallet()).compareTo(updateContractAddQO.getReplenishmentAmount())==-1)){
                        return new DhResponse(ResponseCodeEnum.CONTRACT_INSUFFICIENT_BALANCE.getResponseCode());
                    }
                    break;
                default:
                    break;
            }

        }

        //判断当前产品是否有人已经选中
        String finishOrderProductId = updateContractAddQO.getFinishOrderProductId();
        FinishOrderProduct finishOrderProduct = finishOrderProductService.selectFinishOrderProductById(finishOrderProductId);
        //兑换状态 0:未兑换；1:已兑换
        if("1".equals(finishOrderProduct.getExchangeStatus())){
            return new DhResponse(ResponseCodeEnum.PRODUCT_REDEEMED.getResponseCode());
        }
        Boolean result = upgradeContractService.addUpgradeContract(updateContractAddQO);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),result);
    }

    /***
     * @Description:获取补款升级合同详情
     * @Param: contractId 合同id
     * @Author: luojie
     * @Date: 2021/9/24 15:46
     */
    @GetMapping("/getUpgradeContractDetail")
    public DataResponse getUpgradeContractDetail(@RequestParam(value="contractId",required = true)String contractId){
        UpdateContractVO updateContractVO = upgradeContractService.getUpgradeContractDetail(contractId);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),updateContractVO);
    }

    /***
     * @Description:补款续费合同查询原来主合同（三个月内）
     * @Param:
     * @Author: luojie
     * @Date: 2021/11/5 15:08
     */
    @GetMapping("/getLastThreeMonthsContractList")
    public DataResponse getLastThreeMonthsContractList(){
        List<ContractLastThreeMonthVO> updateContractVO = upgradeContractService.getLastThreeMonthsContractList();
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),updateContractVO);
    }
}
